package com.example.birthday_getway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

@Component
public class SafeGatewayRoutePrinter implements CommandLineRunner {

  // 使用@Autowired(required = false)允许依赖为空
  @Autowired(required = false)
  private RouteDefinitionLocator routeLocator;

  @Autowired(required = false)
  private DiscoveryClient discoveryClient;

  @Override
  public void run(String... args) {
    if (routeLocator != null) {
      Flux<RouteDefinition> routes = routeLocator.getRouteDefinitions();
      routes.subscribe(
          route -> {
            System.out.println("\n[网关路由] ID: " + route.getId());
            System.out.println("URI: " + route.getUri());
          });
    } else {
      System.out.println("RouteDefinitionLocator 不可用 - 请检查Gateway依赖");
    }

    if (discoveryClient != null) {
      System.out.println("\n[服务发现] 服务列表: " + discoveryClient.getServices());
    } else {
      System.out.println("DiscoveryClient 不可用 - 请检查服务发现配置");
    }
  }
}
